public class hasCycle {
    /**
     * 判断环形链表
     */
    public class ListNode{
        int val;
        ListNode next;
        public ListNode(){}
        public ListNode(int val,ListNode next){
            this.val=val;
            this.next=next;
        }
        ListNode(int x){
            val = x;
            next=null;
        }
    }

    public boolean method(ListNode head){
        // 先进行特例的判断
        if(head==null ||head.next==null){
            return false;
        }
        ListNode slow=head;
        ListNode fast=head.next;
        while(slow!=fast){
            if(fast==null){
                return false;
            }
            slow=slow.next;
            fast=fast.next.next;
        }
        return true;
    }
    public static void main(String[] args) {
        hasCycle h=new hasCycle();
        ListNode headWithCycle = h.new ListNode(3);
        headWithCycle.next = h.new ListNode(2);
        headWithCycle.next.next =h.new ListNode(0);
        headWithCycle.next.next.next = h.new ListNode(-4);
        headWithCycle.next.next.next.next = headWithCycle.next; // 设置成环

        boolean res=h.method(headWithCycle);
        System.out.println(res);
    }
}
